home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / overview / dtscpluslibrary / sources / volume.cp < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.9 KB  |  235 lines

  1. /*
  2.     File:        Volume.cp
  3.  
  4.     Contains:    TVolume is a simple volume based utility class (provides information about volumes)     
  5.                   TVolume.cp contains the TVolume member functions. 
  6.  
  7.     Written by: Kent Sandvik    
  8.  
  9.     Copyright:    Copyright © 1993-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 8/18/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #ifndef _VOLUME_
  25. #include "Volume.h"
  26. #endif
  27.  
  28. // CONSTRUCTORS & DESTRUCTORS
  29. #pragma segment Volume
  30. TVolume::TVolume()
  31. {
  32.     // Assume we will start with the default boot volume.
  33.     fVRefNum = this->GetBootVRefNum();
  34.     fIndex = 1;                                    // boot block index
  35. }
  36.  
  37.  
  38. #pragma segment Volume
  39. TVolume::TVolume(short refNum)
  40. {
  41.     // Use specified refNum.
  42.     fVRefNum = refNum;
  43. }
  44.  
  45.  
  46. #pragma segment Volume
  47. TVolume::~TVolume()
  48. {
  49. }
  50.  
  51.  
  52. // MAIN INTERFACE
  53. #pragma segment Volume
  54. short TVolume::GetBootVRefNum()
  55. // Return out the VRefNum of the boot volume.
  56. {
  57.     short ourVRefNum = 0;
  58.     long ourDirID = 0;
  59.  
  60.     fError = ::FindFolder(kOnSystemDisk, kSystemFolderType, kDontCreateFolder, &ourVRefNum, &ourDirID);
  61.     VASSERT(fError == noErr, ("Problems with FindFolder = %d", fError));
  62.  
  63.     return ourVRefNum;
  64. }
  65.  
  66.  
  67. #pragma segment Volume
  68. long TVolume::GetKFreeSpace()
  69. // Get amount of free space (Kb) on defined volume.
  70. {
  71.     if (this->GetHParamBlock())
  72.         return ((fPB.volumeParam.ioVFrBlk * fPB.volumeParam.ioVAlBlkSiz) / 1024);
  73.     else
  74.         return -1;
  75. }
  76.  
  77.  
  78. #pragma segment Volume
  79. long TVolume::GetFileCount()
  80. // Return amount of files on volume.
  81. {
  82.     if (this->GetHParamBlock())
  83.         return fPB.volumeParam.ioVFilCnt;
  84.     else
  85.         return -1;
  86. }
  87.  
  88.  
  89. #pragma segment Volume
  90. long TVolume::GetDirCount()
  91. // Return amount of folders on volume.
  92. {
  93.     if (this->GetHParamBlock())
  94.         return fPB.volumeParam.ioVDirCnt;
  95.     else
  96.         return -1;
  97. }
  98.  
  99.  
  100. #pragma segment Volume
  101. long TVolume::GetCreationDate()
  102. // Return creation date of volume.
  103. {
  104.     if (this->GetHParamBlock())
  105.         return fPB.volumeParam.ioVCrDate;
  106.     else
  107.         return -1;
  108. }
  109.  
  110.  
  111. #pragma segment Volume
  112. short TVolume::GetDriverNumber()
  113. // Get specified driver number.
  114. {
  115.     if (this->GetHParamBlock())
  116.         return fPB.volumeParam.ioVDrvInfo;
  117.     else
  118.         return 0;
  119. }
  120.  
  121.  
  122. #pragma segment Volume
  123. Boolean TVolume::GetName(Str255& name)
  124. // Get name of volume.
  125. {
  126.  
  127.     fPB.volumeParam.ioNamePtr = (StringPtr)name;
  128.     fPB.volumeParam.ioVRefNum = fVRefNum;
  129.     fPB.volumeParam.ioVolIndex = fIndex;
  130.  
  131.     fError = ::PBHGetVInfoSync(&fPB);
  132.     VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
  133.  
  134.     return (fError == noErr);
  135. }
  136.  
  137.  
  138. #pragma segment Volume
  139. short TVolume::GetNumVolumes()
  140. // Get the amount of mounted volumes (at this instance).
  141. {
  142.     short num = 0;
  143.  
  144.     for (this->Reset(); !this->Last(); this->Next())
  145.         num++;
  146.  
  147.     return num;
  148. }
  149.  
  150.  
  151. // GET/SET FUNCTIONS
  152. #pragma segment Volume
  153. void TVolume::SetVRefNum(const short refNum)
  154. // Set used VRefNum.
  155. {
  156.     fVRefNum = refNum;
  157. }
  158.  
  159.  
  160. #pragma segment Volume
  161. short TVolume::GetVRefNum() const
  162. // Get used VRefNum.
  163. {
  164.     return fVRefNum;
  165. }
  166.  
  167.  
  168. // ITERATORS
  169. #pragma segment Volume
  170. void TVolume::Reset()
  171. // Get back to boot block references.
  172. {
  173.     fVRefNum = this->GetBootVRefNum();            // boot block VRefNum
  174.     fIndex = 1;                                    // boot block index
  175.     fLast = false;                                // don't know that yet
  176.     this->Next();                                // step once to the right position (boot drive)
  177. }
  178.  
  179.  
  180. #pragma segment Volume
  181. Boolean TVolume::Last()
  182. // Return boolan if this is the last volume or not.
  183. {
  184.     return fLast;
  185. }
  186.  
  187.  
  188. #pragma segment Volume
  189. void TVolume::Next()
  190. // Iterate to next volume mounted.
  191. {
  192.     if (!fLast)
  193.     {
  194.         fPB.volumeParam.ioNamePtr = NULL;
  195.         fPB.volumeParam.ioVRefNum = fVRefNum;
  196.         fPB.volumeParam.ioVolIndex = fIndex;
  197.  
  198.         fError = ::PBHGetVInfoSync(&fPB);
  199.  
  200.         if (fError != nsvErr)                    // "No such volume" indicates that we are the end
  201.         {
  202.             fIndex++;                            // OK, increase the index
  203.             fVRefNum = fPB.volumeParam.ioVRefNum;// keep track of the VRefNum
  204.         }
  205.         else
  206.             fLast = true;                        // we hit the end, raise the flag
  207.     }
  208. }
  209.  
  210.  
  211. // INTERNAL FUNCTIONS
  212. #pragma segment Volume
  213. Boolean TVolume::GetHParamBlock()
  214. // Generate a lookup in a pre-defined param block (used by other member functions).
  215. {
  216.     fPB.volumeParam.ioNamePtr = NULL;
  217.     fPB.volumeParam.ioVRefNum = fVRefNum;
  218.     fPB.volumeParam.ioVolIndex = 0;
  219.  
  220.     fError = ::PBHGetVInfoSync(&fPB);
  221.     VASSERT(fError == noErr, ("Problems with PBHGetVInfo = %d", fError));
  222.  
  223.     return (fError == noErr);
  224. }
  225.  
  226.  
  227. // _________________________________________________________________________________________________________ //
  228.  
  229.  
  230. /*    Change History (most recent last):
  231.   No        Init.    Date        Comment
  232.   1            khs        1/5/93        New file
  233.   2            khs        1/7/93        Cleanup
  234. */
  235.